<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue'

// 响应式数据
const guide = ref(null)
const loading = ref(true)
const error = ref(null)

// 返回列表页面
function backToList() {
  uni.navigateBack()
}

onMounted(() => {
    // 获取页面传递的参数
    try {
        const instance = getCurrentInstance().proxy;
        // 正确获取eventChannel的方式
        const eventChannel = instance.getOpenerEventChannel();
        if (eventChannel) {
            eventChannel.on('guideData', (data) => {
                console.log('接收到的指南数据:', data)
                guide.value = data
                loading.value = false
            })
            
            // 设置超时检查，确保数据能够被接收
            setTimeout(() => {
                if (loading.value && !guide.value) {
                console.error('未能接收到指南数据')
                error.value = '加载数据失败，请返回重试'
                loading.value = false
                }
            }, 1000)
        } else {
            console.error('无法获取事件通道')
            error.value = '页面加载错误，请返回重试'
            loading.value = false
        }
    } catch (e) {
        console.error('获取页面参数失败:', e)
        error.value = '页面加载错误，请返回重试'
        loading.value = false
    }
})
</script>

<template>
  <view class="container">
    <view class="detail-header">
      <view class="back-btn" @click="backToList">返回</view>
      <view class="detail-title">{{ guide?.title || '健康指南详情' }}</view>
    </view>
    
    <view v-if="loading" class="loading-container">
      <text>加载中...</text>
    </view>
    
    <view v-else-if="error" class="error-container">
      <text class="error-text">{{ error }}</text>
      <view class="retry-btn" @click="backToList">返回列表</view>
    </view>
    
    <view v-else-if="guide" class="detail-content">
      <image class="detail-image" :src="guide.image || '/static/placeholder.png'" mode="widthFix" />
      <view class="detail-text">{{ guide.content }}</view>
    </view>
    
    <view v-else class="error-container">
      <text class="error-text">未找到指南数据</text>
      <view class="retry-btn" @click="backToList">返回列表</view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.container {
  padding: 30rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.detail-header {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
  
  .back-btn {
    font-size: 28rpx;
    color: #1890ff;
    margin-right: 20rpx;
  }
  
  .detail-title {
    font-size: 34rpx;
    font-weight: bold;
    color: #333;
  }
}

.detail-content {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  
  .detail-image {
    width: 100%;
    border-radius: 8rpx;
    margin-bottom: 20rpx;
  }
  
  .detail-text {
    font-size: 28rpx;
    color: #333;
    line-height: 1.6;
  }
}

.loading-container,
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx;
  background-color: #fff;
  border-radius: 12rpx;
  min-height: 300rpx;
}

.error-text {
  font-size: 28rpx;
  color: #ff4d4f;
  margin-bottom: 30rpx;
}

.retry-btn {
  font-size: 28rpx;
  color: #1890ff;
  padding: 10rpx 30rpx;
  border: 1px solid #1890ff;
  border-radius: 30rpx;
}
</style>